package ee.emhi;

import android.app.Activity;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebView.PictureListener;
import android.widget.Button;
import android.widget.RadioButton;

public class Satelliidid extends Activity implements OnClickListener {

	WebView wVSatelite = null;
	Button bForward, bBackward, bAuto, bPause;
	RadioButton bInfra, bClouds;
	int pictureNr;
	CountDownTimer timer;
	boolean satChoice;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.satelliidid);
		initialize();

	}

	@SuppressWarnings("deprecation")
	private void initialize() {
		bInfra = (RadioButton) findViewById(R.id.bInfra);
		bClouds = (RadioButton) findViewById(R.id.bClouds);
		bForward = (Button) findViewById(R.id.bForward);
		bBackward = (Button) findViewById(R.id.bBackward);
		bAuto = (Button) findViewById(R.id.bAuto);
		bPause = (Button) findViewById(R.id.bPause);
		wVSatelite = (WebView) findViewById(R.id.wView);
		wVSatelite.getSettings().setBuiltInZoomControls(true);
		wVSatelite.getSettings().setLoadWithOverviewMode(true);
		wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_01.jpg");
		//wVSatelite.setInitialScale(42);
		wVSatelite.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		wVSatelite.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
		pictureNr = 1;
		satChoice=true;
		
		/*
		 * wVSatelite.setPictureListener(new PictureListener() {
		 * 
		 * public void onNewPicture(WebView view, Picture picture) { // TODO
		 * Auto-generated method stub wVSatelite.scrollTo(150, 0); } });
		 */
		bInfra.setOnClickListener(this);
		bClouds.setOnClickListener(this);
		bForward.setOnClickListener(this);
		bBackward.setOnClickListener(this);
		bAuto.setOnClickListener(this);
		bPause.setOnClickListener(this);
		
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bBackward:
			picBackwards();

			break;
		case R.id.bForward:
			picForward();

			break;

		case R.id.bAuto:
			bAuto.setVisibility(View.GONE);
			bPause.setVisibility(View.VISIBLE);
			autoSurf();
			timer.start();

			break;

		case R.id.bPause:
			bPause.setVisibility(View.GONE);
			bAuto.setVisibility(View.VISIBLE);
			timer.cancel();

			break;
		case R.id.bInfra:
			wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/radar/SUR_CAP_02.gif");
			satChoice=false;
			pictureNr=1;
			bInfra.setChecked(true);
			bClouds.setChecked(false);
			break;

		case R.id.bClouds:
			wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_01.jpg");
			satChoice=true;
			pictureNr=1;
			bClouds.setChecked(true);
			bInfra.setChecked(false);
			break;
		}

	}

	private void picBackwards() {
		if (satChoice){
			if (pictureNr == 1) {
				pictureNr = 12;
			}
			if (pictureNr < 11) {
				pictureNr -= 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_0"
						+ pictureNr + ".jpg");
	
			} else {
				pictureNr -= 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_"
						+ pictureNr + ".jpg");
				if (pictureNr == 12)
					pictureNr = 1;
			}
		}
		else {
			if (pictureNr == 1) {
				pictureNr = 12;
			}
			if (pictureNr < 11) {
				pictureNr -= 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/radar/SUR_CAP_0"
						+ pictureNr + ".gif");

			} else {
				pictureNr -= 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/radar/SUR_CAP_"
						+ pictureNr + ".gif");
				if (pictureNr == 12)
					pictureNr = 1;
			}
		}
	}

	private void picForward() {
		if (satChoice){
			if (pictureNr < 9) {
	
				pictureNr += 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_0"
						+ pictureNr + ".jpg");
	
			} else {
				pictureNr += 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/sat/ir108_"
						+ pictureNr + ".jpg");
				if (pictureNr == 12)
					pictureNr = 1;
			}
		}
		else{
			if (pictureNr < 9) {

				pictureNr += 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/radar/SUR_CAP_0"
						+ pictureNr + ".gif");

			} else {
				pictureNr += 1;
				wVSatelite.loadUrl("http://www.emhi.ee/ilma_andmed/radar/SUR_CAP_"
						+ pictureNr + ".gif");
				if (pictureNr == 12)
					pictureNr = 1;
			}
		}
	}

	private void autoSurf() {
		timer = new CountDownTimer(500, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				Log.v("Hei", "seconds remaining: " + millisUntilFinished / 1000);
			}

			@Override
			public void onFinish() {
				picForward();
				timer.start();
			}

		};
	}
}
